﻿using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using System.Web; 

namespace SX.Utils
{
    public static class AuthHelper
    {
        public static string CookieName = ".rxsec";
        //private static JSONParameters Parameter;
        //static AuthHelper() {
        //    Parameter = new JSONParameters { EnableAnonymousTypes = true, UseFastGuid = true, SerializeNullValues = false, DateTimeMilliseconds = true, UseUTCDateTime = false };
        //}
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <param name="realName">姓名</param>
        /// <param name="is_sup">是否超级管理员</param>
        /// <returns></returns>
        public static async Task SignInAsync(string userid, string realName,int is_sup)
        {
            //var data = JsonHelper.ToJson(userData);


            var context = SxHttpContext.Current;
            if (context == null)
                throw new InvalidOperationException();
            var claims = new List<Claim> {
                new Claim("userid", userid), 
                new Claim("realname", realName)
            };
            if (is_sup == 1)
            {
                claims.Add(new Claim("is_sup", "1"));
            }
            var claimsIdentity = new ClaimsIdentity(
                claims,
                CookieAuthenticationDefaults.AuthenticationScheme);

            await context.SignInAsync(
                CookieAuthenticationDefaults.AuthenticationScheme,
                new ClaimsPrincipal(claimsIdentity),
                new AuthenticationProperties
                {
                    IsPersistent = true,
                    ExpiresUtc = DateTime.UtcNow.AddDays(7)
                });
        }

        public static async Task SingOutAsync()
        {
            await SxHttpContext.Current.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
            //FormsAuthentication.SignOut();
        }
  
        //public static T GetUserData<T>() where T : class, new()
        //{
        //    T UserData = null;
        //    try
        //    {
        //        var context = SxHttpContext.Current;
        //        if (!context.User.Identity.IsAuthenticated)
        //        {
        //            return null;
        //        }
        //        var userData = context.User.Claims.Where(c => c.Type == "userData").ToList();
        //        if (userData != null && userData.Count>0)
        //        {
        //            UserData = JsonHelper.ToObject<T>(userData[0].Value);
        //        }  
        //    }
        //    catch
        //    { }

        //    return UserData;
        //}

        /// <summary>
        /// 是否登录
        /// </summary>
        /// <returns></returns>
        public static bool IsAuth() {
            var context = SxHttpContext.Current;
            if (context.User.Identity.IsAuthenticated)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 获取用户Id
        /// </summary>
        /// <returns></returns>
        public static long GetUserid() {
            string name = "";
            try
            {
                var context = SxHttpContext.Current;
                if (!context.User.Identity.IsAuthenticated)
                {
                    return 0;
                }
                var userData = context.User.Claims.Where(c => c.Type == "userid").ToList();
                if (userData != null && userData.Count > 0)
                {
                    name = userData[0].Value;
                }
            }
            catch
            { } 
            return CommonHelper.ToInt64(name); 
        }

        /// <summary>
        /// 获取用户真实姓名
        /// </summary>
        /// <returns></returns>
        public static string GetUserRealname()
        {
            string name = "";
            try
            {
                var context = SxHttpContext.Current;
                if (!context.User.Identity.IsAuthenticated)
                {
                    return "";
                }
                var userData = context.User.Claims.Where(c => c.Type == "realname").ToList();
                if (userData != null && userData.Count > 0)
                {
                    name = userData[0].Value;
                }
            }
            catch
            { }
            return name;
        }
    }
}
